#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef double db;

#define in read()
#define pii pair<int,int>
#define fi first
#define se second
#define FILE(x) freopen(x".in","r",stdin);\
	freopen(x".out","w",stdout);
#define pb push_back

int read(){
	int x = 0,sgn = 1;char ch = getchar();
	for(;!isdigit(ch);ch = getchar()) if(ch == '-') sgn = -1;
	for(;isdigit(ch);ch = getchar()) x = (x<<1)+(x<<3)+(ch^48);
	return x*sgn;
}

const int N = 310;
const int M = 5010;
const int inf = 0x3f3f3f3f;

int S,T,d[N],vis[N],incf[N],pre[N],h[N],cnt = 1,n,totcost,out[N],to[N];
struct edge{int v,w,c,nxt;}e[M<<2];
void link(int x,int y,int w,int c){e[++cnt] = (edge){y,w,c,h[x]};h[x] = cnt; e[++cnt] = (edge){x,0,-c,h[y]};h[y] = cnt;}

bool spfa(){
	queue<int> q; q.push(S); memset(d,0x3f,sizeof(d)); d[S] = 0; incf[S] = inf; memset(vis,0,sizeof(vis)); vis[S] = 1;
	while(q.size()){
		int x = q.front(); q.pop();
		for(int i = h[x],y;i;i = e[i].nxt){
			if(d[y = e[i].v] > d[x] + e[i].c && e[i].w){
				d[y] = d[x] + e[i].c; incf[y] = min(e[i].w,incf[x]); pre[y] = i;
				if(!vis[y]) q.push(y),vis[y] = 1;
			}
		}
		vis[x] = 0;
	}return d[T] < inf && incf[T] > 0;
}

void update(){
	int x = T;
	while(x ^ S){
		int i = pre[x];
		e[i].w -= incf[T]; e[i^1].w += incf[T]; x = e[i^1].v;
	}totcost += incf[T] * d[T];
}

int main (){
#ifndef ONLINE_JUDGE
	freopen("1.in","r",stdin);
#endif
	n = in; S = 0,T = n + 2;
	for(int i = 1;i <= n;i++){
		int k = in;
		for(int j = 1;j <= k;j++){
			int b = in,t = in; out[i]++; to[b]++; totcost += t;
			link(i,b,inf,t);
		}
	}
	for(int i = 2;i <= n;i++) link(i,n+1,inf,0);
	for(int i = 1;i <= n;i++) if(to[i] > out[i]) link(S,i,to[i]-out[i],0);else link(i,T,out[i]-to[i],0);
	link(n+1,1,inf,0);
	while(spfa()) update();
	printf("%d\n",totcost);
	return 0;
}
